import axios from "axios";
class Ajax {
  constructor(type, url, param, dataType, wait) {
    this.type = (type || "get").toLowerCase();
    this.url = url || '';
    this.dataType = dataType || "application/json;charset=UTF-8";
    this.wait = wait || false; //是否是多个请求
    this.param__ = {};
    if (this.type === "post") {
      axios.defaults.headers.post["Content-Type"] = this.dataType;
      this.param__ = param || {};
      this.param__.lang = localStorage.getItem("lang") || "ch";
    } else {
      this.param__ = {
        params: param || {}
      };
      this.param__.params.lang = localStorage.getItem("lang") || "ch";
    }
  }
  exec (success, error) {
    axios.defaults.headers.common["token"] = localStorage.getItem("token") || "";
    axios[this.type](this.url, this.param__)
      .then(function (ret) {
        if (ret.statusText === "OK" || ret.status === 200) {
          if (ret.data.error == "00000000") {
            success && success(ret.data);
          } else {
            _vue.$message.error(ret.data.msg || "系统异常")
          }

        }
      })
      .catch(function (ret) {
        error && error(ret.data);
      });
  }
}
var h5_native_proxy = {
  proxy: function (module, param, success, error, name) {
    module = module || 'web';
    param = param || {};
    name = name || 'web';
    if (module == "api") {
      var ajax_ = new Ajax(param.mode, param.apiPath, param.data);
      ajax_.exec(function (data) {
        success && success(data);
      }, function (data) {
        error && error(data);
      })
    } else {
      success && success([]);
    }
  }
};

export {
  h5_native_proxy
}
